﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsumeAndProduceFactory
{
    public enum Status { STATUS_STOP, STATUS_PAUSE, STATUS_WORKING };
    public class ConsumeAndProduceFactory : IConsumeAndProduceFactory
    {
        public ConsumeAndProduceFactory(int maxProducer, int maxConsumer, int maxProduct)
        {
            this.maxConsumers = maxConsumer;
            this.maxProducers = maxProducer;
            this.maxProducts = maxProduct;
        }

        public void SetBufferSize(int buffersize)
        {
            this.buffer.MaxBufferNum = buffersize;
        }

        public int startwork()
        {
            buffer = new WareHouse(maxProducts);
            if (MaxProducers == 0 || MaxConsumers == 0)
                return -((MaxProducers == 0 ? 1 : 0) + (MaxConsumers == 0 ? 2 : 0));
            //没有生产者，返回-1
            //没有消费者，返回-2
            //两者皆无，返回-3


            // 初始化消费者、生产者线程 ...
            for (int i = 0; i < MaxProducers; i++)
            {
                Producer obj = new Producer(buffer, Producing);
                producerList.Add(obj);
                obj.start();
            }

            for (int i = 0; i < MaxConsumers; i++)
            {
                Consumer obj = new Consumer(buffer, Consuming);
                obj.OnceConsume = onceConsume;
                consumerList.Add(obj);
                obj.start();
            }

            // 打开进程监视器 ...
            /*if (threadMonitor != null)
            {
                threadMonitor.reset();
                threadMonitor.start();
            }*/
            return 0;
        }

        public void pausework()
        {
            // 暂停消费者、生产者线程 ...
            for (int i = 0; i < producerList.Count; i++)
            {
                Producer p = (Producer)producerList[i];
                p.pause();
            }

            for (int i = 0; i < consumerList.Count; i++)
            {
                Consumer c = (Consumer)consumerList[i];
                c.pause();
            }
        }

        public void stopwork()
        {
            // 暂停消费者、生产者线程 ...
            for (int i = 0; i < producerList.Count; i++)
            {
                Producer p = (Producer)producerList[i];
                p.stop();
            }

            for (int i = 0; i < consumerList.Count; i++)
            {
                Consumer c = (Consumer)consumerList[i];
                c.stop();
            }
        }

        public void resumework()
        {
            // 重启消费者、生产者线程 ...
            for (int i = 0; i < producerList.Count; i++)
            {
                Producer p = (Producer)producerList[i];
                p.resume();
            }

            for (int i = 0; i < consumerList.Count; i++)
            {
                Consumer c = (Consumer)consumerList[i];
                c.resume();
            }
        }

        public void stopImmediately()
        {
            // 立刻停止消费者、生产者线程 ...
            for (int i = 0; i < producerList.Count; i++)
            {
                Producer p = (Producer)producerList[i];
                p.stopImmediately();
            }

            for (int i = 0; i < consumerList.Count; i++)
            {
                Consumer c = (Consumer)consumerList[i];
                c.stopImmediately();
            }
        }

        private int maxProducers;
        public int MaxProducers
        {
            get { return maxProducers; }
            set { maxProducers = value; }
        }

        private int maxConsumers;
        public int MaxConsumers
        {
            get { return maxConsumers; }
            set { maxConsumers = value; }
        }

        private int maxProducts;
        public int MaxProducts
        {
            get { return maxProducts; }
            set { maxProducts = value; }
        }

        private WareHouse buffer;//缓冲区
        private int onceConsume = 1;

        public int OnceConsume
        {
            get { return onceConsume; }
            set { onceConsume = value; }
        }
        public List<IConsumer> consumerList = new List<IConsumer>();//所有消费者
        public List<IProducer> producerList = new List<IProducer>();//所有生产者
        //private ThreadMonitor threadMonitor = new ThreadMonitor();//进程监视者
        public event FactoryEventHandler Producing;
        public event FactoryEventHandler Consuming;
    }
}
